package com.seveneight.baijiesteward.ui.first.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.base.BaseFragmentBack;
import com.seveneight.baijiesteward.base.MessageEvent;
import com.seveneight.baijiesteward.bean.CardBean;
import com.seveneight.baijiesteward.bean.OrderBean;
import com.seveneight.baijiesteward.bean.UserBean;
import com.seveneight.baijiesteward.http.AppConfig;
import com.seveneight.baijiesteward.ui.main.fragment.MainFragment;
import com.seveneight.baijiesteward.ui.main.presenter.LoginPresenter;
import com.seveneight.baijiesteward.ui.user.activity.EditCardActivity;
import com.seveneight.baijiesteward.ui.user.contract.CreateOderContract;
import com.seveneight.baijiesteward.ui.user.fragment.ManagerAccountFragment;
import com.seveneight.baijiesteward.ui.user.presenter.CreateOrderPresenter;
import com.seveneight.baijiesteward.ui.user.activity.ManagerCardActivity;
import com.seveneight.baijiesteward.ui.user.activity.RecordActivity;
import com.seveneight.baijiesteward.ui.user.contract.CardListContract;
import com.seveneight.baijiesteward.ui.user.presenter.CardListPresenter;
import com.seveneight.baijiesteward.utils.StringUtils;
import com.seveneight.baijiesteward.utils.WindowUtil;
import com.seveneight.baijiesteward.views.RightPopupWindow;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by user1 on 2018/3/28.
 */

public class DrawingFragment extends BaseFragmentBack implements View.OnClickListener, CardListContract.CardListView, CreateOderContract.CreateOderView {

    CreateOderContract.CreateOderPresenter createOderPresenter;
    CardListContract.CardListPresenter cardListPresenter;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_wBalance;
    private TextView tv_wBalance1;
    private TextView tv_wAbleBalance;
    private TextView tv_wUnAbleBalance;
    private TextView bt_submit;
    private TextView tv_cardInfo;
    private TextView tv_moreCard;
    private EditText et_inputMoney;
    private EditText et_tracePwd;
    private CardBean cardBean;
//    private UserBean userBean;
    private ImageView iv_right;
    private RightPopupWindow popupWindow;

    public static DrawingFragment newInstance() {
        Bundle args = new Bundle();
        DrawingFragment fragment = new DrawingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_drawing;
    }

    @Override
    protected void initView() {
        tv_title = findView(R.id.tv_title);
        iv_right = findView(R.id.iv_right);
        tv_title.setText("我的收益");
        tv_wBalance1 = findView(R.id.tv_wBalance);
        tv_wAbleBalance = findView(R.id.tv_wAbleBalance);
        tv_wUnAbleBalance = findView(R.id.tv_wUnAbleBalance);
        bt_submit = findView(R.id.bt_submit);
        tv_cardInfo = findView(R.id.tv_cardInfo);
        tv_moreCard = findView(R.id.tv_moreCard);
        et_inputMoney = findView(R.id.et_inputMoney);
        et_inputMoney.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        et_tracePwd = findView(R.id.et_tracePwd);
        iv_back = findView(R.id.iv_back);
    }

    @Override
    protected void initData() {
//        userBean = setting.getUserInfo();
        tv_wBalance1.setText(userBean.getWAbleBalance());
        tv_wAbleBalance.setText(userBean.getWBalance());
        tv_wUnAbleBalance.setText(userBean.getWUnAbleBalance());
        createOderPresenter = new CreateOrderPresenter(this);
        cardListPresenter = new CardListPresenter(this);
        new LoginPresenter(null).getUserInfo("",getContext(),jsonFormat.commonParam());
        cardListPresenter.getCardList(mContext, jsonFormat.commonParam());
    }

    @Override
    public void CallBus(MessageEvent messageEvent) {
        super.CallBus(messageEvent);
        if (messageEvent.getType()==110){
            tv_wBalance1.setText(userBean.getWAbleBalance());
            tv_wAbleBalance.setText(userBean.getWBalance());
            tv_wUnAbleBalance.setText(userBean.getWUnAbleBalance());
        }
    }

    @Override
    protected void setListener() {
        iv_right.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        bt_submit.setOnClickListener(this);
        tv_moreCard.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                _mActivity.onBackPressed();
                break;
            case R.id.tv_moreCard:
                Bundle bundle = new Bundle();
                bundle.putInt("type", 2);
                Intent i = new Intent(mContext, ManagerCardActivity.class);
                i.putExtras(bundle);
                startActivityForResult(i, AppConfig.REQUEST_CODE_1);
                break;
            case R.id.bt_submit:
                if (null ==cardBean){
                    showDialog("请选择收款卡账号");
                    return;
                }
                if (StringUtils.isEmpty(cardBean.getBranchBankCode())){
                    showDialog();
                }else {
                    submit();
                }
                break;
            case R.id.iv_right:
                if(popupWindow==null){
                    popupWindow = new RightPopupWindow(getActivity(),iv_right);
                }
                popupWindow.showDrawingRightWindow();
                break;
        }
    }
    public void showDialog(){
        new MaterialDialog.Builder(getContext())
                .content("请先完善银行卡信息")
                .contentColorRes(R.color.graytext)
                .backgroundColorRes(R.color.white)
                .negativeColorRes(R.color.mainblue)
                .negativeText(R.string.negative)
                .positiveColorRes(R.color.mainblue)
                .positiveText(R.string.positive)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        Bundle bundle2 = new Bundle();
                        bundle2.putSerializable("data", cardBean);
                        Intent n = new Intent(getActivity(), EditCardActivity.class);
                        n.putExtras(bundle2);
                        startActivityForResult(n, AppConfig.REQUEST_CODE_2);
                    }
                })
                .build().show();
    }


    private void submit() {
        String wABalance = userBean.getWAbleBalance();//可提取的余额
        String newBalance = et_inputMoney.getText().toString().trim();//提取的余额
        String trancePwd = et_tracePwd.getText().toString();
        if (wABalance.equals("0.0000")) {
            showDialog("暂时没有余额可提现");
            return;
        }
        if (TextUtils.isEmpty(newBalance)) {
            showDialog("请输入提现金额");
            return;
        }
        double money = Double.parseDouble(wABalance);
        double minMoney = Double.parseDouble(newBalance);
        BigDecimal data1 = new BigDecimal(money);
        BigDecimal data2 = new BigDecimal(minMoney);
        if (data2.compareTo(data1) == 1) {
            showDialog("超出可提现余额");
            return;
        }
        if (cardBean == null) {
            showDialog("请选择收款卡账号");
            return;
        }
        if (TextUtils.isEmpty(trancePwd)) {
            showDialog("请输入交易密码");
            return;
        }
        createOderPresenter.createOrder(mContext, jsonFormat.createOrder(newBalance,
                "0",
                cardBean.getDebitCardID(),
                null,
                null,
                trancePwd, 3));
    }

    @Override
    public void reBackCardList(List<CardBean> data) {
        if (data.size() < 1) {
            tv_cardInfo.setText("暂时没有银行卡");
            tv_moreCard.setText("去添加");
        } else {
            if (null == cardBean){
                cardBean = data.get(0);
                initBottom(cardBean);
            }else {
                for (int i=0;i<data.size();i++){
                    if (data.get(i).getDebitCardID().equals(cardBean.getDebitCardID())){
                        cardBean=data.get(i);
                        break;
                    }
                }
            }
        }
    }

    private void initBottom(CardBean cardBean) {
        tv_cardInfo.setText(cardBean.getBankName() + "(" + cardBean.getBankCardNo().substring(cardBean.getBankCardNo().length() - 4) + ")");
        tv_moreCard.setText("更换");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AppConfig.REQUEST_CODE_2 && resultCode == RESULT_OK) {
            cardListPresenter.getCardList(mContext, jsonFormat.commonParam());
        }
        if (data == null) {
            return;
        }
        if (requestCode == AppConfig.REQUEST_CODE_1 && resultCode == RESULT_OK) {
            cardBean = (CardBean) data.getSerializableExtra("bean");
            initBottom(cardBean);
        }
    }

    @Override
    public void reBackOrderInfo(OrderBean orderBean) {
        showDialog("提交成功");
        _mActivity.onBackPressed();
    }
}
